package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    SetmealMapper setmealMapper;

    @Autowired
    SetmealDishMapper setmealDishMapper;



    @Override
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //插入到setmeal套餐表中
        setmealMapper.insert(setmeal);//插入一条

        //插入到setmeal_dish套餐菜品关系表中
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {//记得设置setmeal_id
            setmealDish.setSetmealId(setmeal.getId());
        });
        setmealDishMapper.insertBatch(setmealDishes);//插入n条
    }

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        //构造分页插件
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        //开始查询
        Page<SetmealVO> page= setmealMapper.page(setmealPageQueryDTO);
        log.info("----");
        //获取total和record封装进pageresult里面
        long total = page.getTotal();
        List<SetmealVO> result = page.getResult();
        //返回pageresult
        return new PageResult(total,result);
    }

    /**
     * 根据套餐ids批量删除套餐
     * @param ids
     */
    @Override
    public void deleteByIds(List<Long> ids) {//套餐ids
        //因为套餐粒度是最细的，所以不用看他有没有关联其他的了
        //1.判断是否为起售状态，如果是起售状态，不能删
        ids.forEach(id->{
            //获取套餐对象,查看其status
            Setmeal setmeal = setmealMapper.getByid(id);
            if(setmeal.getStatus()== StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);//起售中的套餐不能删除
            }

        });
        //可以删
        setmealMapper.deleteByIds(ids);
        //2.删除后要把相关的菜品和关系给删除
        ids.forEach(setmealId->{
            setmealDishMapper.deleteBysetmealId(setmealId);
        });
    }

    /**
     * 根据套餐id查询套餐信息
     * @param id
     */
    @Override
    public SetmealVO getById(Long id) {
        log.info("根据套餐id查询套餐信息");
        //1.查询套餐表信息
        Setmeal setmeal = setmealMapper.getByid(id);
        //2.查询套餐与菜品关系表信息
        List<SetmealDish> setmealDish=setmealDishMapper.getByid(setmeal.getId());
        System.out.println(setmealDish);
        //3.构建setmealvo对象
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(setmealDish);
        //4.返回setmealvo对象
        return setmealVO;
    }

    /**
     * 保存修改套餐信息
     * @param setmealDTO
     */
    @Override
    public void update(SetmealDTO setmealDTO) {
        //1.保存进setmeal套餐表
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);
        //2.将套餐与菜品信息保存进套餐与菜品关系表
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealDTO.getId());//记得更新的时候也要设置setmeal_id
        });
        setmealDishMapper.deleteBysetmealId(setmealDTO.getId());//根据套餐id清空对应的套餐与菜品的关系
        setmealDishMapper.insertBatch(setmealDishes);//批量插入新的--我们在新增套餐的时候已经实现了
    }

    /**
     * 启用禁用套餐
     * @param status
     * @param setmealid
     */
    @Override
    public void startOrStop(Integer status, Long setmealid) {
        Setmeal setmeal = Setmeal.builder()
                .status(status)
                .id(setmealid)
                .build();
        setmealMapper.update(setmeal);
    }


    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealDishMapper.list(setmeal);
        return list;
    }


    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
